# -*- coding: UTF-8 -*-
# __author__ = '10459'

# from flask import Flask,current_app
#
# app = Flask(__name__)
# # ctx = app.app_context()
# # ctx.push()
# # a = current_app
# # b = current_app.config['DEBUG']
#
# # 实现了上下文协议的对象使用with语句
# # 上下文管理器
# # __enter__与__exit__
# # 案例中with后面的是返回了AppContext
# with app.app_context():
#     a = current_app
#     b = current_app.config['DEBUG']
#
# with open("D:\\t.txt") as f:
#     print (f.read())


class MyResource:
    def __enter__(self):
        print("connect to resource")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_tb:
            print("process exception")
        else:
            print("null exception")
        # return True   # 不写这句默认返回False

    def query(self):
        print("query sucess")


try:
    with MyResource() as res:
        a = 1 / 0
        res.query()
except Exception as err:
    print(err)
